package com.situ.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Customer;
import com.situ.crm.entity.CustomerLoss;
import com.situ.crm.entity.Order;
import com.situ.crm.mapper.CustomerLossMapper;
import com.situ.crm.mapper.CustomerMapper;
import com.situ.crm.mapper.OrderMapper;
import com.situ.crm.mapper.UserRoleMapper;
import com.situ.crm.service.ICustomerLossService;

@Service
public class CustomerLossServiceImpl implements ICustomerLossService{
	@Autowired
	private CustomerLossMapper customerLossMapper;
	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private OrderMapper orderMapper;
	
	@Override
	public ServerResponse pageList(Integer page, Integer limit, CustomerLoss customerLoss) {
		//1、使用PageHelper插件设置分页
		PageHelper.startPage(page, limit);
		//2、执行查询
	     List<CustomerLoss> list = customerLossMapper.pageList(customerLoss);
		//3、使用PageInfo对结果进行包装
		PageInfo pageInfo = new PageInfo(list);
		Integer count = (int) pageInfo.getTotal();//得到总数量
		
		return ServerResponse.createSuccess("查询成功", count, list);
	}

	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			int count = customerLossMapper.deleteByPrimaryKey(id);
			if (count == 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	@Override
	public ServerResponse deleteAll(String ids) {
		String[] idArray = ids.split(",");
		int count = customerLossMapper.deleteAll(idArray);
		if (count == idArray.length) {
			return ServerResponse.createSuccess("删除成功");
		} else {
			return ServerResponse.createError("删除失败");
		}
	}

	@Override
	public CustomerLoss selectByPrimaryKey(Integer customerLossId) {
		return customerLossMapper.selectByPrimaryKey(customerLossId);
	}

	@Override
	public ServerResponse add(CustomerLoss customerLoss) {
		try {
			int count = customerLossMapper.insert(customerLoss);
			return ServerResponse.createSuccess();
		} catch (Exception e) {
			return ServerResponse.createError("添加失败");
		}
	}
	
	
	@Override
	public ServerResponse update(CustomerLoss customerLoss) {
		try {
			// 将角色插入到数据库中
			int count = customerLossMapper.updateByPrimaryKey(customerLoss);
			return ServerResponse.createSuccess("更新成功");
		} catch (Exception e) {
			return ServerResponse.createError("更新失败");
		}
	}

	@Override
	public void checkCustomerLoss() {
		System.out.println("CustomerLossServiceImpl.checkCustomerLoss()");
		// 1.查找所有流失的客户(在订单表里面6个月都没有订单)
		List<Customer> customerList = customerMapper.findLossCustomer();
		for (Customer customer : customerList) {
			// 2.实例化客户流失对象
			CustomerLoss customerLoss = new CustomerLoss();
			customerLoss.setCustomerId(customer.getId());// 客户编号
			customerLoss.setCustomerName(customer.getName());// 客户名称
			// 查找这个客户最后一次的订单信息
			Order order = orderMapper.findLastOrderByCustomerId(customer.getId());
			if (order != null) {
				customerLoss.setLastOrderTime(order.getOrderDate());// 客户最后一次订单时间
			} else {
				customerLoss.setLastOrderTime(null);
			}

			// 3.添加到客户流失表里面
			customerLossMapper.insert(customerLoss);
			// 4.客户表里面把客户状态修改为1：流失状态
			customer.setStatus(1);
			customerMapper.updateByPrimaryKeySelective(customer);
		}
	}
}
